package com.vogue.supplymall.order.domain.biz.impl;

import com.google.common.util.concurrent.AtomicLongMap;
import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.dto.OrderInfo;
import com.vogue.supplymall.common.domain.dto.SearchPrepayInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.OrderNumberGenerator;
import com.vogue.supplymall.order.common.CalcOrderItemUtils;
import com.vogue.supplymall.order.common.CheckOrderUtils;
import com.vogue.supplymall.order.domain.biz.OrderBiz;
import com.vogue.supplymall.order.domain.biz.OrderUpdateBiz;
import com.vogue.supplymall.order.domain.biz.PrepaySearchBiz;
import com.vogue.supplymall.order.domain.model.Order;
import com.vogue.supplymall.order.domain.model.OrderBuyer;
import com.vogue.supplymall.order.domain.model.OrderItem;
import com.vogue.supplymall.order.domain.model.OrderItemProduct;
import com.vogue.supplymall.order.domain.model.OrderSeller;
import com.vogue.supplymall.order.domain.model.OrderStatus;
import com.vogue.supplymall.order.domain.service.OrderBuyerService;
import com.vogue.supplymall.order.domain.service.OrderItemProductService;
import com.vogue.supplymall.order.domain.service.OrderItemService;
import com.vogue.supplymall.order.domain.service.OrderSellerService;
import com.vogue.supplymall.order.domain.service.OrderService;
import com.vogue.supplymall.order.domain.service.OrderStatusService;
import com.vogue.supplymall.order.exception.DifferentOrderException;
import com.vogue.supplymall.order.exception.OrderPriceException;
import com.vogue.supplymall.order.exception.SendAmountException;
import com.vogue.supplymall.order.exception.ConfirmOrderItemCheckException;
import com.vogue.supplymall.shop.domain.model.ProductAttrplan;
import com.vogue.supplymall.shop.domain.model.Shop;
import com.vogue.supplymall.shop.domain.service.ProductAttrplanService;
import com.vogue.supplymall.shop.domain.service.ShopService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * The type Order biz.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-01-23
 */
@Service("orderBiz")
public class OrderBizImpl implements OrderBiz {
    /**
     * The constant LOGGER_ORDERBIZIMPL.
     */
    public static final Logger LOGGER_ORDERBIZIMPL = LoggerFactory.getLogger(OrderBizImpl.class);
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private OrderItemProductService orderItemProductService;
    @Resource
    private OrderBuyerService orderBuyerService;
    @Resource
    private OrderSellerService orderSellerService;
    @Resource
    private OrderStatusService orderStatusService;
    @Resource
    private ShopService shopService;
    @Resource
    private OrderUpdateBiz orderUpdateBiz;
    @Resource
    private ProductAttrplanService productAttrplanService;
    @Resource
    private PrepaySearchBiz prepaySearchBiz;

    @Override
    public Map<String, Object> findOrder(OrderInfo orderInfo, UserInfo userInfo) {
        if (carItemCheckIsExist(orderInfo)) {
            throw new ConfirmOrderItemCheckException("同一仓库下不同规格的商品不能提交订单");
        }
        LOGGER_ORDERBIZIMPL.info("findOrder 查询提交订单信息");
        List<Map<String, Object>> order = orderItemProductService.findOrder(orderInfo);
        Map<String, Object> data = new HashMap<>();
        /*计算订单*/
        orderSettings(order, data, orderInfo.getAddress(), userInfo);
        /*订单合计金额不能超过%s万元*/
        //BigDecimal orderPrice = new BigDecimal(data.get("orderprice").toString());
        // 取消订单合计不能大于2万的check
//        if (orderPrice.compareTo(ConstantBean.MAX_ORDER_PRICE) > 0) {
//            LOGGER_ORDERBIZIMPL.info("addOrder 订单合计金额不能超过2万元");
//            throw new OrderPriceException(ConstantBean.MAX_ORDER_PRICE_CHAR);
//        }
        for (Map<String, Object> map : order) {
            if ("1".equals(map.get("s_invalid").toString())
                    || "1".equals(map.get("p_invalid").toString())
                    || "1".equals(map.get("invalid").toString())) {
                LOGGER_ORDERBIZIMPL.info("addOrder 订单信息验证失败，订单中的商品有更新！");
                throw new DifferentOrderException(orderInfo.getGroupName() + "-" + orderInfo.getShopName());
            }
        }
        data.put("goodsData", order);
        return data;
    }

    /**
     * 校验购物车商品是否有同一仓库多规格情况
     * @param orderInfo
     * @return
     */
    private Boolean carItemCheckIsExist(OrderInfo orderInfo){
        List<Map<String, Object>> goodsData = orderInfo.getGoodsData();
        Map<String, Object> pid_storageMap = new HashMap<>();
        String pid_storage = "";
        for (Map<String, Object> map : goodsData) {
            pid_storage = map.get("pid").toString()+ "_" + map.get("storageid").toString();
            if(pid_storageMap.containsKey(pid_storage)){
                return true;
            } else {
                pid_storageMap.put(pid_storage,pid_storage);
            }
        }
        return false;
    }

    /**
     * 设置和计算订单内容
     * 配送范围、订单及订单项目价格
     *
     * @param order
     * @param data
     * @param userInfo
     * @param address
     */
    private void orderSettings(List<Map<String, Object>> order, Map<String, Object> data, Map<String, String> address, UserInfo userInfo) {
        String mcid = null;
        /*编辑可配送范围*/
        for (Map<String, Object> product : order) {
            // 2018/10/25 添加卖家的注册号id和卖家的机构id
            data.put("sellsyscodeid", product.get("sellsyscodeid"));
            data.put("sellorganid", product.get("sellorganid"));
            if (product.get("mcid") != null) {
                mcid = product.get("mcid").toString();
            }
            if (null != address) {
                switch (product.get("sendlvl").toString()) {
                    case "2":
                        product.put("sendFlag", product.get("area").toString().equals(address.get("area")));
                        break;
                    case "4":
                        product.put("sendFlag", product.get("area").toString().equals(address.get("province")));
                        break;
                    case "8":
                        product.put("sendFlag", product.get("area").toString().equals(address.get("city")));
                        break;
                    default:
                        product.put("sendFlag", true);
                        break;
                }
            }
            /*计算商品价格*/
            CalcOrderItemUtils.calcOrder(product);
            // 2018/10/29 查询预付款余额
            SearchPrepayInfo searchPrepayInfo = new SearchPrepayInfo();
            searchPrepayInfo.setSyscodeid(userInfo.getSyscodeid());
            searchPrepayInfo.setOrganid(userInfo.getOrganInfo().getId());
            searchPrepayInfo.setSellorganid(new Long(product.get("sellorganid").toString()));
            searchPrepayInfo.setSellsyscodeid(new Long(product.get("sellsyscodeid").toString()));
            Map<String, String> prepay = prepaySearchBiz.searchPrepayBalance(searchPrepayInfo);
            data.put("prepaidValue", prepay.get("balance"));
            data.put("paymode", prepay.get("paymode"));
        }

        /*订单总数量*/
        Integer totalquantity = order.parallelStream()
                .map((product) -> new Integer(product.get("itemquantity").toString()))
                .reduce(0, (acc, x) -> acc + x);
        /*订单总价格*/
        BigDecimal totalprice = order.parallelStream()
                .map((product) -> new BigDecimal(product.get("itemprice").toString()))
                .reduce(BigDecimal.ZERO, (acc, x) -> acc.add(x));
        data.put("totalquantity", totalquantity);
        data.put("totalprice", totalprice);
        /*订单运费*/
        BigDecimal freight = calcFreight(order);
        data.put("freight", freight);
        /*订单合计价格*/
        data.put("orderprice", totalprice.add(freight));
        data.put("mcid", mcid);
    }

    private BigDecimal calcFreight(List<Map<String, Object>> order) {
        BigDecimal freight = BigDecimal.ZERO;
        /*获取店铺运费信息*/
        Function<Map<String, Object>, Map<String, Object>> getShopFreightInfo = (firstData) -> firstData
                .entrySet()
                .stream()
                .filter((entry) -> entry.getKey().startsWith("s_"))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        Map<String, Object> shopFreightInfo = getShopFreightInfo.apply(order.get(0));

        /*获取计件商品运费计算*/
        Function<List<Map<String, Object>>, BigDecimal> getSingleFreight = (List<Map<String, Object>> list) -> list
                .stream()
                .filter((map) -> "1".equals(map.get("p_freighttype").toString()))
                .map((map) -> {
                    // 起送件数
                    BigDecimal p_freight = new BigDecimal(map.get("p_freight").toString());
                    // 起送件费用
                    BigDecimal p_freightamount = new BigDecimal(map.get("p_freightamount").toString());
                    // 续件
                    BigDecimal p_freightstep = new BigDecimal(map.get("p_freightstep").toString());
                    // 续件费用
                    BigDecimal p_freightstepamount = new BigDecimal(map.get("p_freightstepamount").toString());
                    // 实际邮寄件数
                    BigDecimal itemquantity = new BigDecimal(map.get("itemquantity").toString());
                    BigDecimal quantity = itemquantity;
                    if (itemquantity.compareTo(p_freight) > 0) {
                        quantity = itemquantity.subtract(p_freight);
                    } else {
                        return p_freightamount;
                    }
                    if (p_freightstep.compareTo(BigDecimal.ZERO) <= 0) {
                        return p_freightamount;
                    }
                    try {
                        BigDecimal f = p_freightamount
                                .add(quantity
                                        .divide(p_freightstep, 2, BigDecimal.ROUND_HALF_UP)
                                        .multiply(p_freightstepamount));
                        return f.setScale(2, BigDecimal.ROUND_HALF_UP);
                    } catch (ArithmeticException e) {
                        LOGGER_ORDERBIZIMPL.error(e.getMessage());
                        LOGGER_ORDERBIZIMPL.error("error：Arithmetic Exception，计件商品运费计算异常！");
                        return p_freightamount;
                    }
                })
                .reduce(BigDecimal.ZERO, (acc, x) -> acc.add(x));

        /*订单中计重商品的总重量*/
        BigDecimal weight = order
                .parallelStream()
                .filter((map) -> "0".equals(map.get("p_freighttype").toString()))
                .map((product) -> new BigDecimal(product.get("weight").toString()).multiply(new BigDecimal(product.get("itemquantity").toString())))
                .reduce(BigDecimal.ZERO, (acc, x) -> acc.add(x));

        /*计重商品运费计算*/
        if (BigDecimal.ZERO.compareTo(weight) < 0) {
            // 起重
            BigDecimal s_freight = new BigDecimal(shopFreightInfo.get("s_freight").toString());
            // 起重费用
            BigDecimal s_freightamount = new BigDecimal(shopFreightInfo.get("s_freightamount").toString());
            // 续重
            BigDecimal s_freightstep = new BigDecimal(shopFreightInfo.get("s_freightstep").toString());
            // 续重费用
            BigDecimal s_freightstepamount = new BigDecimal(shopFreightInfo.get("s_freightstepamount").toString());
//            BigDecimal mod = weight.compareTo(s_freight) > 0 ? weight.subtract(s_freight) : BigDecimal.ZERO;
            if (weight.compareTo(s_freight) > 0) {
                weight = weight.subtract(s_freight);
            } else {
                return s_freightamount;
            }
            if (s_freightstep.compareTo(BigDecimal.ZERO) <= 0) {
                return s_freightamount;
            }
            try {
                BigDecimal f = s_freightamount.add((weight
                        .subtract(s_freight))
                        .divide(s_freightstep, 2, BigDecimal.ROUND_HALF_UP)
                        .multiply(s_freightstepamount));
                freight = freight.add(f);
            } catch (ArithmeticException e) {
                LOGGER_ORDERBIZIMPL.error(e.getMessage());
                LOGGER_ORDERBIZIMPL.error("error：Arithmetic Exception，计件商品运费计算异常！");
                freight = s_freightamount;
            }
        }
        BigDecimal singleFreight = getSingleFreight.apply(order);
        return freight.add(singleFreight).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    @Transactional
    public Map<String, Long> addOrder(OrderInfo orderInfo, UserInfo userInfo) {
        LOGGER_ORDERBIZIMPL.info("验证提交订单信息 start");
        AtomicLongMap<String> longMap = AtomicLongMap.create();
        /*检索最新的订单信息*/
        List<Map<String, Object>> current = orderItemProductService.findOrder(orderInfo);
        if (null == current) {
            throw new RuntimeException();
        }
        Map<String, Object> data = new HashMap<>();
        /*计算订单*/
        orderSettings(current, data, orderInfo.getAddress(), userInfo);
        /*判断店铺的起送金额*/
        BigDecimal sendamount = new BigDecimal(current.stream().findFirst().get().get("sendamount").toString());
        BigDecimal orderPrice = new BigDecimal(data.get("orderprice").toString());
        if (sendamount.compareTo(orderPrice) > 0) {
            throw new SendAmountException(orderInfo.getGroupName() + "-" + orderInfo.getShopName(), sendamount.toString());
        }
        /*订单合计金额不能超过%s万元*/
        if (orderPrice.compareTo(ConstantBean.MAX_ORDER_PRICE) > 0) {
            LOGGER_ORDERBIZIMPL.info("addOrder 订单合计金额不能超过500万元");
            throw new OrderPriceException(ConstantBean.MAX_ORDER_PRICE_CHAR);
        }

        /*验证前端回传的订单信息*/
        if (CheckOrderUtils.checkOrderDifferent(current, orderInfo.getGoodsData())) {
            LOGGER_ORDERBIZIMPL.info("addOrder 订单信息验证失败，订单中的商品有更新！");
            throw new DifferentOrderException(orderInfo.getGroupName() + "-" + orderInfo.getShopName());
        }
        /*提交订单*/
        createOrder(orderInfo, userInfo, current, data, longMap);
        LOGGER_ORDERBIZIMPL.info("提交订单信息 end");
        //如果是判断出用预付货款直接付款提交订单则在同一事物里保证预付货款支付成功 -- begin
//        if (orderPrice.compareTo(BigDecimal.ZERO) > 0 && orderInfo.getPaytype() != null && orderInfo.getPaytype().intValue() == 4) {
//            orderInfo.setOid(String.valueOf(longMap.get("orderid")));
//            orderUpdateBiz.payAdvance(orderInfo);
//        }
        //如果是判断出用预付货款直接付款提交订单则在同一事物里保证预付货款支付成功 -- end
        return longMap.asMap();
    }

    @Override
    @Transactional
    public List<Map<String, Long>> addMultiOrder(List<OrderInfo> orderInfos, UserInfo userInfo) {
        List<Map<String, Long>> list = new ArrayList<>();
        for (OrderInfo orderInfo : orderInfos) {
            list.add(addOrder(orderInfo, userInfo));
        }
        return list;
    }

    /**
     * 创建订单信息
     *
     * @param orderInfo
     * @param userInfo
     * @param current
     * @param data
     */
    private void createOrder(OrderInfo orderInfo, UserInfo userInfo, List<Map<String, Object>> current, Map<String, Object> data, AtomicLongMap<String> longMap) {
        LOGGER_ORDERBIZIMPL.info("createOrder 提交订单信息 start");
        /*添加订单信息*/
        Order order = insertOrder(orderInfo, userInfo, data);
        longMap.put("orderid", order.getId());
        /*添加订单状态信息*/
        OrderStatus status = insertOrderStatus(userInfo, order.getId());
        longMap.put("statusid", status.getId());
        /*添加订单买家*/
        insertOrderBuyer(orderInfo, userInfo, order.getId());
        /*添加订单卖家*/
        insertOrderSeller(orderInfo, userInfo, order.getId());
        /*添加订单项目和商品*/
        for (Map<String, Object> item : current) {
            Integer inventory = updateOrderItem(item, order.getId());
            Long attrplanId = updateOrderItemProduct(item, order.getId());
            /*减库存*/
            updateProductAttr(attrplanId, inventory);
        }
        LOGGER_ORDERBIZIMPL.info("createOrder 提交订单信息 end");
    }

    /**
     * @param id
     * @param inventory
     */
    private void updateProductAttr(Long id, Integer inventory) {
        ProductAttrplan attrplan = productAttrplanService.get(id);
        if (null == attrplan) {
            throw new RuntimeException("");
        }
        attrplan.setInventory(attrplan.getInventory().subtract(new BigDecimal(inventory)));
        attrplan.setModifyDate(new Date());
        int count = productAttrplanService.update(attrplan);
        if (count <= 0) {
            throw new RuntimeException("");
        }
        LOGGER_ORDERBIZIMPL.info("updateProductAttr 减库存 end");
    }

    /**
     * 插入订单信息
     *
     * @param orderInfo
     * @param userInfo
     * @param data
     * @return order
     */
    private Order insertOrder(OrderInfo orderInfo, UserInfo userInfo, Map<String, Object> data) {
        LOGGER_ORDERBIZIMPL.info("create order table");
        Order order = new Order();
        order.setSyscodeid(userInfo.getSyscodeid());
        order.setOrdernumber(OrderNumberGenerator.getter());
        order.setCommitflag(1);
        if (StringUtils.isNotEmpty(orderInfo.getMemo())) {
            order.setMemo(orderInfo.getMemo());
        } else {
            order.setMemo("");
        }
        order.setFreight(new BigDecimal(data.get("freight").toString()));
        order.setAdjustprice(BigDecimal.ZERO);
        /*订单总件数*/
        order.setTotalquantity(new Integer(data.get("totalquantity").toString()));
        /*订单总金额*/
        order.setTotalprice(new BigDecimal(data.get("totalprice").toString()));
        /*最终价格*/
        order.setFinalprice(order.getTotalprice().add(order.getFreight()));
        return orderService.save(order);
    }

    /**
     * 插入订单状态信息
     *
     * @param userInfo
     */
    private OrderStatus insertOrderStatus(UserInfo userInfo, Long orderId) {
        /*add order status*/
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderid(orderId);
        orderStatus.setOrderstatus(0);
        orderStatus.setIscancel(false);
        orderStatus.setSyscodeid(userInfo.getSyscodeid());
        orderStatus.setPullstate(0);
        // 2018/7/2 添加买家拉取状态
        orderStatus.setBuypullstate(0);
        return orderStatusService.save(orderStatus);
    }

    /**
     * 插入订单买家信息
     *
     * @param orderInfo
     * @param userInfo
     * @param orderId
     * @return OrderBuyer
     */
    private OrderBuyer insertOrderBuyer(OrderInfo orderInfo, UserInfo userInfo, Long orderId) {
        LOGGER_ORDERBIZIMPL.info("create order_buyer table");
        OrderBuyer buyer = new OrderBuyer();
        buyer.setOrderid(orderId);
        buyer.setSyscodeid(userInfo.getSyscodeid());
        if (null != userInfo.getOrganInfo()) {
            buyer.setOrganid(userInfo.getOrganInfo().getId());
        } else {
            // 个人情况下插入登录id
            buyer.setOrganid(userInfo.getLoginId());
        }
        buyer.setUserid(userInfo.getLoginId());
        buyer.setBuyername(orderInfo.getAddress().get("name"));
        String address = orderInfo.getAddress().get("provincename") + orderInfo.getAddress().get("cityname") + orderInfo.getAddress().get("addr");
        // 存入买家地址信息
//        buyer.setAddressinfo(new Gson().toJson(orderInfo.getAddress()));
        buyer.setBuyeraddress(address);
        buyer.setBtel(orderInfo.getAddress().get("tel"));
        buyer.setBuyertype(userInfo.getOrganInfo() == null ? 0 : 1);
        /*2018-2-28添加matchid*/
        if (null != userInfo.getOrganInfo()) {
            buyer.setMatchid(userInfo.getOrganInfo().getMatchid());
        } else {
            buyer.setMatchid(userInfo.getLoginId().toString());
        }
        if (null == userInfo.getOrganInfo()) {
            buyer.setEntname(userInfo.getTel());
            buyer.setStorename(userInfo.getTel());
        } else {
            buyer.setEntname(userInfo.getEntName());
            buyer.setStorename(userInfo.getOrganInfo().getName());
        }
        buyer.setSyscode(userInfo.getSyscode());

        return orderBuyerService.save(buyer);
    }

    /**
     * 插入订单卖家信息
     *
     * @param orderInfo
     * @param userInfo
     * @param orderId
     * @return OrderSeller
     */
    private OrderSeller insertOrderSeller(OrderInfo orderInfo, UserInfo userInfo, Long orderId) {
        LOGGER_ORDERBIZIMPL.info("create order_seller table");
        OrderSeller seller = new OrderSeller();
        seller.setOrderid(orderId);
        seller.setShopid(orderInfo.getShopId());
        Shop shop = shopService.get(orderInfo.getShopId());
        if (null != shop) {
            seller.setSyscodeid(shop.getSyscodeid());
            seller.setOrganid(shop.getOrganid());
            seller.setShopname(shop.getName());
            seller.setEntname(shop.getEntname());
            seller.setAddress(shop.getAddress());
            seller.setStel(shop.getTel());
            seller.setMatchid(shop.getMatchid());
            seller.setMcid(shop.getMcid());
        } else {
            throw new RuntimeException("");
        }
        return orderSellerService.save(seller);
    }

    /**
     * 更新购物车项目为订单项目
     *
     * @param orderItemInfo
     * @param orderId
     */
    private Integer updateOrderItem(Map<String, Object> orderItemInfo, Long orderId) {
        LOGGER_ORDERBIZIMPL.info("update order_item table");
        OrderItem item = orderItemService.get(orderItemInfo.get("oiid").toString());
        if (null == item) {
            throw new RuntimeException("");
        }
        item.setOrderid(orderId);
        item.setStorageId(new Long(orderItemInfo.get("storageid").toString()));
        item.setStorage(orderItemInfo.get("storage").toString());
        item.setItemquantity(new Integer(orderItemInfo.get("itemquantity").toString()));
        item.setItemprice(new BigDecimal(orderItemInfo.get("itemprice").toString()));
        item.setModifyDate(new Date());
        /* add 2018/7/26 发货数量 发货金额 差异数量 差异金额 */
        BigDecimal qty = new BigDecimal(item.getItemquantity());
        item.setSendcount(qty);
        item.setSendmoney(item.getItemprice());
        item.setDiffcount(BigDecimal.ZERO);
        item.setDiffmoney(BigDecimal.ZERO);
        int count = orderItemService.update(item);
        if (count <= 0) {
            throw new RuntimeException("");
        }
        return item.getItemquantity();
    }

    /**
     * 更新购物车项目的商品
     *
     * @param orderItemInfo
     * @param orderId
     */
    private Long updateOrderItemProduct(Map<String, Object> orderItemInfo, Long orderId) {
        LOGGER_ORDERBIZIMPL.info("update order_item_product table");
        OrderItemProduct itemProduct = orderItemProductService.get(new Long(orderItemInfo.get("oipid").toString()));
        if (null == itemProduct) {
            throw new RuntimeException("");
        }
        itemProduct.setOrderid(orderId);
        itemProduct.setPname(orderItemInfo.get("pname").toString());
        itemProduct.setPunit(orderItemInfo.get("unit_name").toString());
        itemProduct.setImgurl(orderItemInfo.get("imgurl").toString());
        itemProduct.setPmatchid(orderItemInfo.get("pmatchid").toString());
        itemProduct.setUmatchid(orderItemInfo.get("unit_matchid").toString());
        /*2018/4/3 如果类型是计件则插入重量为0*/
        if ("1".equals(orderItemInfo.get("p_freighttype").toString())) {
            itemProduct.setWeight(BigDecimal.ZERO);
        } else {
            itemProduct.setWeight(new BigDecimal(orderItemInfo.get("weight").toString()));
        }
        if (orderItemInfo.containsKey("public_price")) {
            itemProduct.setPrice(new BigDecimal(orderItemInfo.get("public_price").toString()));
        }
        if (orderItemInfo.containsKey("private_price")) {
            itemProduct.setPrice(new BigDecimal(orderItemInfo.get("private_price").toString()));
        }
        itemProduct.setModifyDate(new Date());
        itemProduct.setItemquantity(new Integer(orderItemInfo.get("itemquantity").toString()));
        itemProduct.setMainproductflag(true);
        int count = orderItemProductService.update(itemProduct);
        if (count <= 0) {
            throw new RuntimeException("");
        }
        return itemProduct.getAttrplanid();
    }

}